#include "pch.h"

/// NEW FILE
/// Handles the meters and counters shown on the screen.
///
/// ----------
/// From vCurrent
/// ----------
/// Known Bugs:
/// * FPS Counter seg faults when trying to load information
///   (Maybe because of the Timer updating at a certain tick?)
///   - FPS Counter handled in FPSCounter Class temporarily
///

std::unique_ptr<Interface> Interface::sInstance (new Interface());

Interface::Interface()
{
    //fpsCounter = NULL;
    elapsedTimeMeter = NULL;
    rotationMeter = NULL;
    powerMeter = NULL;
    distanceMeter = NULL;
    XSpeedMeter = NULL;
    YSpeedMeter = NULL;

    font = NULL;
    textColor = {};

    fpsShown = false;
    interfaceShown = true;
}

Interface::~Interface()
{
    //dtor
}

bool Interface::init()
{
    if(sInstance)
    {
        font = GraphicsDevice::sInstance->getFont();
        textColor = GraphicsDevice::sInstance->getTextColor();
        return true;
    }
    return false;
}

void Interface::show_object(Cannon* cannon)
{
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2, 0, 0, 0, elapsedTimeMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2, GraphicsDevice::sInstance->getScreenHeight() / 1.05, 0, 0, rotationMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2 + 150, GraphicsDevice::sInstance->getScreenHeight() / 1.05, 0, 0, powerMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2 + 300, GraphicsDevice::sInstance->getScreenHeight() / 1.05, 0, 0, distanceMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2, GraphicsDevice::sInstance->getScreenHeight() / 1.1, 0, 0, XSpeedMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2 + 150, GraphicsDevice::sInstance->getScreenHeight() / 1.1, 0, 0, YSpeedMeter, GraphicsDevice::sInstance->getGameScreen());
    GraphicsDevice::sInstance->apply_surface(cannon->getWidth() / 2 + 150, 0, 0, 0, highScoreMeter, GraphicsDevice::sInstance->getGameScreen());
}

void Interface::move_object()
{

}

void Interface::clear_surface()
{
    //SDL_FreeSurface(fpsCounter);
    SDL_FreeSurface(rotationMeter);
    SDL_FreeSurface(powerMeter);
    SDL_FreeSurface(distanceMeter);
    SDL_FreeSurface(XSpeedMeter);
    SDL_FreeSurface(YSpeedMeter);
}

void Interface::calculateMeter(Cannon* cannon, Projectile* projectile)
{
    // Time Meter
    std::stringstream elapsedTime;
    elapsedTime << "Time Elapsed: " << (FPSCounter::sInstance->elapsed.getTicks() / 1000);
    elapsedTimeMeter = TTF_RenderText_Blended(font, ((elapsedTime.str())).c_str(), textColor);

    // Rotation Meter
    std::stringstream rotationText;
    rotationText << "Rotation: " << cannon->getRotation() + 45.f;
    rotationMeter = TTF_RenderText_Blended(font, (rotationText.str()).c_str(), textColor);

    // Power Meter
    std::stringstream powerText;
    powerText << "Power: " << cannon->getPower();
    powerMeter = TTF_RenderText_Blended(font, (powerText.str()).c_str(), textColor);

    // Distance Meter
    std::stringstream distanceText;
    distanceText << "Distance: " << projectile->getTrueX();
    distanceMeter = TTF_RenderText_Blended(font, (distanceText.str()).c_str(), textColor);

    // X Speed Meter
    std::stringstream xText;
    xText << "X Speed: " << projectile->getXVel();
    XSpeedMeter = TTF_RenderText_Blended(font, (xText.str()).c_str(), textColor);

    // Y Speed Meter
    std::stringstream yText;
    yText << "Y Speed: " << (projectile->getYVel() * -1);
    YSpeedMeter = TTF_RenderText_Blended(font, (yText.str()).c_str(), textColor);

    // High Score Meter
    std::stringstream highScoreText;
    highScoreText << "High Score: " << cannon->getHighScore();
    highScoreMeter = TTF_RenderText_Blended(font, (highScoreText.str()).c_str(), textColor);
}

/* Unused
void Interface::toggleFps()
{
    fpsShown = (!fpsShown);
}
*/

void Interface::toggleInterface()
{
    interfaceShown = (!interfaceShown);
}
